<script setup>
// 问题1：100万个数字,位置打乱，在控制台看结果
// 问题1：方法1
let numberCounter = 1000000
let numbers = Array.from({ length: numberCounter }, (_, i) => i + 1)
numbers.sort(() => Math.random() - 0.5)

console.log('问题1：100万个数字,位置打乱')
console.log('问题1：方法1 numbers:', numbers)

// 问题1：方法2
numbers = Array(numberCounter)
  .fill()
  .map((_, i) => i + 1)
numbers.sort(() => Math.random() - 0.5)

console.log(' ')
console.log('问题1：方法2 numbers:', numbers)

// 问题1：方法3
numbers = Array.from({ length: numberCounter }, (_, i) => i + 1)
let uniqueNumbers = [...new Set(numbers)]
uniqueNumbers.sort(() => Math.random() - 0.5)

console.log(' ')
console.log('问题1：方法3 uniqueNumbers:', uniqueNumbers)

/////////////////////////////////////////////////////////////////////////////////////////
// 问题2：2个数组获取相同和不同的数字
// 问题2：方法1
let array1 = [1, 2, 3, 4, 5]
let array2 = [4, 5, 6, 7, 8]

console.log(' ')
console.log('问题2：2个数组获取相同和不同的数字')
console.log('array1:', array1)
console.log('array2:', array2)

let commonElements = array1.filter((value) => array2.includes(value))
let uniqueToArray1 = array1.filter((value) => !array2.includes(value))
let uniqueToArray2 = array2.filter((value) => !array1.includes(value))

console.log(' ')
console.log('问题2：方法1 Common:', commonElements)
console.log('Unique to Array1:', uniqueToArray1)
console.log('Unique to Array2:', uniqueToArray2)

// 问题2：方法2
let set1 = new Set(array1)
let set2 = new Set(array2)

commonElements = [...set1].filter((x) => set2.has(x))
uniqueToArray1 = [...set1].filter((x) => !set2.has(x))
uniqueToArray2 = [...set2].filter((x) => !set1.has(x))

console.log(' ')
console.log('问题2：方法2 Common:', commonElements)
console.log('Unique to Array1:', uniqueToArray1)
console.log('Unique to Array2:', uniqueToArray2)

// 问题2：方法3
commonElements = array1.reduce((acc, val) => {
  if (array2.includes(val)) {
    acc.push(val)
  }
  return acc
}, [])

uniqueToArray1 = array1.reduce((acc, val) => {
  if (!array2.includes(val)) {
    acc.push(val)
  }
  return acc
}, [])

uniqueToArray2 = array2.reduce((acc, val) => {
  if (!array1.includes(val)) {
    acc.push(val)
  }
  return acc
}, [])

console.log(' ')
console.log('问题2：方法3 Common:', commonElements)
console.log('Unique to Array1:', uniqueToArray1)
console.log('Unique to Array2:', uniqueToArray2)
</script>

<template>
  <div></div>
</template>

<style scoped></style>
